package com.ss.android.article.browser.download.internal;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

import com.ss.android.article.browser.download.DownloadTaskManager;
import com.ss.android.browser.base.wrapper.a.BrowserSharedPreferencesWrapper;
import com.ss.android.common.app.AbsApplication;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;

public final class DownloadOperator extends AsyncTask {
    public DownloadTask mDownloadTask;
    public DownloadTaskManager mDownloadTaskManager;
    public volatile boolean c = false;
    public volatile boolean d = false;
    public boolean e = false;
    private boolean f = true;

    public DownloadOperator(DownloadTaskManager downloadTaskManager, DownloadTask downloadTask) {
        this.mDownloadTask = downloadTask;
        this.mDownloadTaskManager = downloadTaskManager;
        Log.d("DownloadOperator", "file path : " + this.mDownloadTask.fileDir);
        Log.d("DownloadOperator", "file name : " + this.mDownloadTask.fileName);
        Log.d("DownloadOperator", "download url : " + this.mDownloadTask.url);
    }

    public DownloadOperator(DownloadTaskManager downloadTaskManager, DownloadTask downloadTask, byte extra) {
        this.mDownloadTask = downloadTask;
        this.mDownloadTaskManager = downloadTaskManager;
        this.f = false;
        Log.d("DownloadOperator", "file path : " + this.mDownloadTask.fileDir);
        Log.d("DownloadOperator", "file name : " + this.mDownloadTask.fileName);
        Log.d("DownloadOperator", "download url : " + this.mDownloadTask.url);
    }

    public final void startDownload() {
        Log.i("DownloadOperator", "start download.");
        this.c = false;
        this.d = false;
        this.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Object[0]);
    }

    private Boolean b() {
        Boolean v3_7;
        DownloadListener v3_6;
        Iterator v6_2;
        int v14;
        long v12;
        byte[] buffer;
        long v10;
        InputStream v3_2 = null;
        RandomAccessFile v9 = null;
        DownloadListener v2_5;
        Iterator v3_1;
        long v6;
        int contentLength;
        URLConnection conn = null;
        this.e = true;
        try {
            conn = new URL(this.mDownloadTask.url).openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestProperty("Accept-Encoding", "musixmatch");
            ((HttpURLConnection) conn).setRequestMethod("HEAD");
            contentLength = conn.getContentLength();
            Log.i("DownloadOperator", "total size[" + contentLength + "]");
            v6 = ((long) contentLength);
            this.mDownloadTask.totalSize = contentLength;
            ((HttpURLConnection) conn).disconnect();
            File v2_4 = new File(this.mDownloadTask.fileDir);
            if (!v2_4.exists()) {
                v2_4.mkdirs();
            }

            v2_4 = new File(this.mDownloadTask.fileDir + "/" + this.mDownloadTask.fileName);
            if (!v2_4.exists()) {
                v2_4.createNewFile();
                this.mDownloadTask.finishedSize = 0;
            }

            RandomAccessFile v4 = new RandomAccessFile(v2_4, "rwd");
            Log.d("DownloadOperator", "fileSize:" + contentLength);
            if (contentLength > 0) {
                v4.setLength(v6);
            }

            v4.close();
        } catch (MalformedURLException v2_2) {
            Log.e("DownloadOperator", "createFile MalformedURLException", v2_2);
        } catch (FileNotFoundException v2_1) {
            Log.e("DownloadOperator", "createFile FileNotFoundException", v2_1);
            v3_1 = this.mDownloadTaskManager.e(this.mDownloadTask).iterator();
            while (v3_1.hasNext()) {
                ((DownloadListener) v3_1.next()).e(this.mDownloadTask);
            }

            v2_5 = this.mDownloadTaskManager.e;
            if (v2_5 == null) {
//                goto label_74;
            } else {
                v2_5.e(this.mDownloadTask);
            }
        } catch (IOException v2) {
            Log.e("DownloadOperator", "createFile IOException", v2);
            v3_1 = this.mDownloadTaskManager.e(this.mDownloadTask).iterator();
            while (v3_1.hasNext()) {
                ((DownloadListener) v3_1.next()).e(this.mDownloadTask);
            }
            v2_5 = this.mDownloadTaskManager.e;
            if (v2_5 == null) {
//                goto label_74;
            } else {
                v2_5.e(this.mDownloadTask);
            }
        }

        label_74:
        this.mDownloadTask.downloadState = DownloadState.DOWNLOADING;
        this.mDownloadTaskManager.updateDatabase(this.mDownloadTask);
        v3_1 = this.mDownloadTaskManager.e(this.mDownloadTask).iterator();
        while (v3_1.hasNext()) {
            ((DownloadListener) v3_1.next()).b(this.mDownloadTask);
        }

        v2_5 = this.mDownloadTaskManager.e;
        if (v2_5 != null) {
            v2_5.b(this.mDownloadTask);
        }

        long v4_1 = 0;
        try {
            conn = new URL(this.mDownloadTask.url).openConnection();
            contentLength = 5000;
            conn.setConnectTimeout(contentLength);
            conn.setRequestProperty("Accept-Encoding", "musixmatch");
            ((HttpURLConnection) conn).setRequestMethod("GET");
            conn.setRequestProperty("Range", "bytes=" + this.mDownloadTask.finishedSize + "-" + this.mDownloadTask.totalSize);
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
            v9 = new RandomAccessFile(this.mDownloadTask.fileDir + "/" + this.mDownloadTask.fileName, "rwd");
            v9.seek(this.mDownloadTask.finishedSize);
            v6 = this.mDownloadTask.finishedSize;
            v10 = this.mDownloadTask.totalSize;
            v3_2 = conn.getInputStream();
            Log.d("DownloadOperator", "downloadListeners size=" + this.mDownloadTaskManager.e(this.mDownloadTask).size());
            Log.i("DownloadOperator", "start writing data to file.");
            buffer = new byte[4096];
            v12 = System.currentTimeMillis();
            v4_1 = v6;
            while (true) {
                v14 = v3_2.read(buffer);
                if (v14 == -1) {
                    break;
                }

                if (!this.c) {
                    v4_1 += ((long) v14);
                    v9.write(buffer, 0, v14);
                    if (v4_1 - this.mDownloadTask.finishedSize > 102400) {
                        this.mDownloadTask.finishedSize = v4_1;
                        this.mDownloadTaskManager.updateDatabase(this.mDownloadTask);
                        this.publishProgress(new Integer[]{
                                Integer.valueOf(((int) v4_1)),
                                Integer.valueOf(((int) v10)),
                                Integer.valueOf(((int) ((v4_1 - v6) / (((long) (((int) (System.currentTimeMillis() + 1 - v12))))))))});
                        continue;
                    }
                    if (v10 - v4_1 >= 102400) {
                        continue;
                    }
                    this.mDownloadTask.finishedSize = v4_1;
                    this.publishProgress(new Integer[]{
                            Integer.valueOf(((int) v4_1)),
                            Integer.valueOf(((int) v10)),
                            Integer.valueOf(((int) ((v4_1 - v6) / (((long) (((int) (System.currentTimeMillis() + 1 - v12))))))))});
                    continue;
                }

                Log.i("DownloadOperator", "pause download, exit download loop.");
                this.mDownloadTask.downloadState = DownloadState.PAUSE;
                this.mDownloadTask.finishedSize = v4_1;
                v6_2 = this.mDownloadTaskManager.e(this.mDownloadTask).iterator();
                try {
                    while (v6_2.hasNext()) {
                        ((DownloadListener) v6_2.next()).c(this.mDownloadTask);
                    }
                    v3_6 = this.mDownloadTaskManager.e;
                    if (v3_6 != null) {
                        v3_6.c(this.mDownloadTask);
                    }

                    this.mDownloadTaskManager.updateDatabase(this.mDownloadTask);
                    v3_7 = Boolean.valueOf(false);
                    if (v3_2 != null) {
                        v3_2.close();
                    }
                    v9.close();
                    if (conn == null) {
                        return v3_7;
                    }
                    ((HttpURLConnection) conn).disconnect();
                } catch (Exception v2_7) {
                    v2_7.printStackTrace();
                }
                return false;
            }

            ((HttpURLConnection) conn).disconnect();
            this.mDownloadTask.downloadState = DownloadState.FINISHED;
            this.mDownloadTask.finishedSize = v4_1;
            Log.d("DownloadOperator", "finished " + this.mDownloadTask);
            this.mDownloadTaskManager.updateDatabase(this.mDownloadTask);
            v6_2 = this.mDownloadTaskManager.e(this.mDownloadTask).iterator();
            while (v6_2.hasNext()) {
                ((DownloadListener) v6_2.next()).a(this.mDownloadTask);
            }
            v3_6 = this.mDownloadTaskManager.e;
            if (v3_6 != null) {
                v3_6.a(this.mDownloadTask);
            }
            this.mDownloadTaskManager.e(this.mDownloadTask).clear();
            this.mDownloadTaskManager.mDownloadListenerMap.remove(this.mDownloadTask);
            v3_7 = Boolean.valueOf(true);
            if (v3_2 != null) {
                v3_2.close();
            }
            try {
                v9.close();
                if (conn == null) {

                } else {
                    ((HttpURLConnection) conn).disconnect();
                }
            } catch (Exception v2_7) {
                v2_7.printStackTrace();
            }
            return v3_7;
        } catch (Exception v3_4) {
            try {
                Log.e("DownloadOperator", "download exception : " + v3_4.getMessage());
                v3_4.printStackTrace();
                this.mDownloadTask.downloadState = DownloadState.FAILED;
                this.mDownloadTask.finishedSize = v4_1;
                Iterator v4_2 = this.mDownloadTaskManager.e(this.mDownloadTask).iterator();
                while (v4_2.hasNext()) {
                    ((DownloadListener) v4_2.next()).e(this.mDownloadTask);
                }
                v2_5 = this.mDownloadTaskManager.e;
                if (v2_5 != null) {
                    v2_5.e(this.mDownloadTask);
                }
                this.mDownloadTaskManager.updateDatabase(this.mDownloadTask);
                if (v3_2 != null) {
                    v3_2.close();
                }
                if (v9 != null) {
                    v9.close();
                }

                if (conn == null) {
                    return false;
                }
                ((HttpURLConnection) conn).disconnect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return false;
        } finally {
            if (v3_2 != null) {
                try {
                    v3_2.close();
                } catch (Exception e) {

                }
            }

            if (v9 != null) {
                try {
                    v9.close();
                } catch (Exception e) {

                }
            }

            if (conn != null) {
                try {
                    ((HttpURLConnection) conn).disconnect();
                } catch (Exception e) {

                }
            }
        }
    }

    protected final Object doInBackground(Object[] arg2) {
        return this.b();
    }

    protected final void onCancelled() {
        super.onCancelled();
        this.e = false;
    }

    protected final void onPostExecute(Object arg3) {
        super.onPostExecute(arg3);
        this.e = false;
        if (!this.f) {
            BrowserSharedPreferencesWrapper.putString("down_tmp_apk", this.mDownloadTask.getFilePath());
            return;
        }

        if (arg3 != null && (((Boolean) arg3).booleanValue())) {
            try {
                Intent v0_1 = android.support.design.a.s(this.mDownloadTask.getFilePath());
                if (v0_1 == null) {
                    return;
                }
                AbsApplication.getApplication().startActivity(v0_1);
            } catch (Exception v0) {
            }
        }
    }

    protected final void onProgressUpdate(Object[] arg10) {
        super.onProgressUpdate(arg10);
        int v6 = ((Integer) arg10[0]).intValue();
        int v7 = ((Integer) arg10[1]).intValue();
        Iterator v8 = this.mDownloadTaskManager.e(this.mDownloadTask).iterator();
        while (v8.hasNext()) {
            ((DownloadListener) v8.next()).a(this.mDownloadTask, ((long) v6), ((long) v7));
        }

        DownloadListener v0 = this.mDownloadTaskManager.e;
        if (v0 != null) {
            v0.a(this.mDownloadTask, ((long) v6), ((long) v7));
        }
    }
}

